package cn.handy.api.service.impl;

import cn.handy.api.entity.UserEntity;
import cn.handy.api.service.IOrderService;
import cn.handy.feign.OrderFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hanshuai
 * @Description: {}
 * @date 2019/3/22 14:21
 */
@Api("订单服务controller")
@RestController
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderFeign orderFeign;

    /**
     * 普通调用member方法
     * @param name
     * @return
     */
    @Override
    @ApiOperation("订单服务调用会员服务,未开启Hystrix")
    @ApiParam(name = "name",value = "String",required = true)
    @GetMapping("/orderToMember")
    public String orderToMember(@RequestParam("name") String name) {
        System.out.println("order调用member的普通服务,线程名字:" + Thread.currentThread().getName());
        UserEntity userEntity = orderFeign.getMember(name);
        return userEntity == null ? "调用错误" : userEntity.toString();
    }

    /**
     * 开启了Hystrix-服务熔断,服务降级,服务隔离
     * @return
     */
    @Override
    @ApiOperation("订单服务调用会员服务,开启了Hystrix")
    @ApiParam(name = "name",value = "String",required = true)
    @HystrixCommand(fallbackMethod = "orderHystrix")
    @GetMapping("/orderToMemberHystrix")
    public String orderToMemberHystrix() {
        System.out.println("order调用member的Hystrix服务,线程名字:" + Thread.currentThread().getName());
        String str = orderFeign.testHystrix();
        return "order调用member的Hystrix服务:返回:" + str;
    }


    /**
     * order自己的方法
     * @return
     */
    @Override
    @ApiOperation("order自己的方法")
    @GetMapping("/orderTest")
    public String orderTest() {
        return "不带参数的测试方法,线程名字:" + Thread.currentThread().getName();
    }

    /**
     * 服务降级方法
     * @return
     */
    public String orderHystrix() {
        return "您好,访问人数过多,正在进行排队,请稍后...";
    }
}
